package com.yy.wga.mp.module.income.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.mp.module.income.response.IncomeUpGradeGetNotifyResponse;
import com.yy.wga.mp.module.income.response.IncomeUpGradePageResponse;
import com.yy.wga.mp.module.income.response.IncomeUpGradeParentUserPageResponse;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.income.entity.pojo.IncomeUpGradeDO;
import com.yy.wga.mp.module.income.mapper.IncomeUpGradeMapper;
import com.yy.wga.mp.module.income.request.IncomeOrderConfirmRequest;
import com.yy.wga.mp.module.income.service.IncomeUpGradeService;
import com.yy.wga.mp.module.user.entity.pojo.UserBaseDO;
import com.yy.wga.mp.module.user.service.UserBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 升级礼包商品收益信息(IncomeUpGrade)表服务实现类
 *
 * @author guochao
 * @since 2024-12-21 20:44:00
 */
@Service
@RequiredArgsConstructor
public class IncomeUpGradeServiceImpl extends ServiceImpl<IncomeUpGradeMapper, IncomeUpGradeDO> implements IncomeUpGradeService {
    private final UserBaseService userBaseService;
    @Override
    public List<IncomeUpGradePageResponse> pageIncome(BasePageRequestDTO request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), false);
        List<IncomeUpGradeDO> incomeUpGradeList = baseMapper.selectList(new LambdaQueryWrapper<IncomeUpGradeDO>()
                .eq(IncomeUpGradeDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(IncomeUpGradeDO::getUserId, RequestUserHolder.getUserCache().getUserId())
                .orderByDesc(IncomeUpGradeDO::getId));
        if (CollectionUtils.isEmpty(incomeUpGradeList)) {
            return Collections.emptyList();
        }
        List<IncomeUpGradePageResponse> responseList = new ArrayList<>(incomeUpGradeList.size());
        // 查询用户手机
        Set<Long> userIdSet = incomeUpGradeList.stream()
                .map(IncomeUpGradeDO::getParentUserId).collect(Collectors.toSet());
        List<UserBaseDO> userBaseList = userBaseService.listByIds(userIdSet);
        Map<Long, UserBaseDO> userMap = userBaseList.stream()
                .collect(Collectors.toMap(UserBaseDO::getId, Function.identity()));
        for (IncomeUpGradeDO incomeUpGrade : incomeUpGradeList) {
            IncomeUpGradePageResponse response = BeansUtils.map(incomeUpGrade, IncomeUpGradePageResponse.class);
            UserBaseDO userBaseDO = userMap.get(incomeUpGrade.getFromUserId());
            if (userBaseDO != null) {
                response.setNickName(userBaseDO.getNickName());
                response.setAvatarUrl(userBaseDO.getAvatarUrl());
            }
            responseList.add(response);
        }
        return responseList;
    }

    @Override
    public List<IncomeUpGradeParentUserPageResponse> parentUserList(BasePageRequestDTO request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), false);
        List<IncomeUpGradeDO> incomeUpGradeList = baseMapper.selectList(new LambdaQueryWrapper<IncomeUpGradeDO>()
                .eq(IncomeUpGradeDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(IncomeUpGradeDO::getParentUserId, RequestUserHolder.getUserCache().getUserId())
                .orderByDesc(IncomeUpGradeDO::getId));
        if (CollectionUtils.isEmpty(incomeUpGradeList)) {
            return Collections.emptyList();
        }
        List<IncomeUpGradeParentUserPageResponse> responseList = new ArrayList<>(incomeUpGradeList.size());
        // 查询订单状态
        // 查询用户手机
        Set<Long> userIdSet = incomeUpGradeList.stream()
                .map(IncomeUpGradeDO::getUserId).collect(Collectors.toSet());
        List<UserBaseDO> userBaseList = userBaseService.listByIds(userIdSet);
        Map<Long, UserBaseDO> userMap = userBaseList.stream()
                .collect(Collectors.toMap(UserBaseDO::getId, Function.identity()));
        for (IncomeUpGradeDO incomeUpGrade : incomeUpGradeList) {
            IncomeUpGradeParentUserPageResponse response = BeansUtils.map(incomeUpGrade, IncomeUpGradeParentUserPageResponse.class);
            UserBaseDO userBaseDO = userMap.get(incomeUpGrade.getFromUserId());
            if (userBaseDO != null) {
                response.setNickName(userBaseDO.getNickName());
                response.setAvatarUrl(userBaseDO.getAvatarUrl());
            }
            responseList.add(response);
        }
        return responseList;
    }

    @Override
    public void confirm(IncomeOrderConfirmRequest request) {
        this.update(new LambdaUpdateWrapper<IncomeUpGradeDO>()
                .eq(IncomeUpGradeDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(IncomeUpGradeDO::getId, request.getId())
                .set(IncomeUpGradeDO::getParentUserConfirm, 1));
    }

    @Override
    public List<IncomeUpGradeGetNotifyResponse> getNotify() {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<IncomeUpGradeDO>()
                .eq(IncomeUpGradeDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(IncomeUpGradeDO::getFromUserId, RequestUserHolder.getUserCache().getUserId())
                .eq(IncomeUpGradeDO::getParentUserConfirm, 0));
        if (count == 0) {
            return Collections.emptyList();
        }
        List<IncomeUpGradeGetNotifyResponse> responseList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            IncomeUpGradeGetNotifyResponse response = new IncomeUpGradeGetNotifyResponse();
            response.setContent("你有一个服务商的大礼包待领取");
            responseList.add(response);
        }
        return responseList;
    }
}
